//
//  PPAccountManager.m
//  pickpic
//
//  Created by Jinxiao on 9/10/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAccountManager.h"

#import "PPAlbumManager.h"

#import "PPNetworkService.h"

@implementation PPAccountManager


+ (instancetype)sharedInstance {
    static id instance;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        instance = self.new;
    });

    return instance;
}

- (instancetype)init {
    self = [super init];

    [[DGAccountStore sharedInstance] loadAccountsFromLocal];

    [[DGAccountManager sharedInstance] reloadCurrentAccount];

    return self;
}

- (void)loginWithAccount:(DGAccount *)account completion:(void (^)(BOOL))completion {

    [[DGAccountStore sharedInstance] saveAccount:account options:DGAccountStoreUpdateAccountCredential completion:^(BOOL success, NSError *error) {
        if(!error) {
            [[DGAccountManager sharedInstance] switchAccount:account completion:^(BOOL success, NSError *error) {
                if(!error) {
                    !completion ? : completion(YES);
                } else {
                    [[DGAccountStore sharedInstance] removeAccount:account options:0 completion:nil];

                    !completion ? : completion(NO);
                }
            }];
        } else {
            [[DGAccountStore sharedInstance] removeAccount:account options:0 completion:nil];

            !completion ? : completion(NO);
        }
    }];

}

- (void)logoutWithCompletion:(void (^)(BOOL))completion {
    DGAccount *account = self.account;

    [[DGAccountManager sharedInstance] switchAccount:nil completion:^(BOOL success, NSError *error) {
        if(!error) {
            [[DGAccountStore sharedInstance] removeAccount:account options:0 completion:nil];

            [account.profiles delete];
            
            [account.profiles.managedObjectContext saveContext];

            [[PPAlbumManager sharedInstance] clean];

            [PPUserAccount clearAccount];
            
            [[DGAccount currentAccount] clearPhone];
             [[DGAccount currentAccount] clear3rdURL];
            
            !completion ? : completion(YES);
        } else {
            !completion ? : completion(NO);
        }
    }];
}

- (void)updateProfilesWithCompletion:(void (^)(void))completion {

    PPUserID userID = self.account.userID;

    if(![self.account validateAccount]) {
        !completion ? : completion();

        return;
    }

    [PPNetworkService fetchPublicInfoWithTargetUserID:userID callback:^(id data, NSError *error) {

        if(error == nil) {

            if([userID isEqual:[data validStringForKey:@"id"]]) {

                PPAccountProfiles *profiles = [PPAccountProfiles existingProfilesForUserID:userID];

                [profiles mergeParameters:data];

                [profiles.managedObjectContext saveContext];

            }

        }

        !completion ?: completion();
    }];
}

- (DGAccount *)account {
    DGAccount *account = DGCurrentAccount;

    return account;
}

@end
